package com.yanqu.road.server.cmd.cross.marriage;

import com.yanqu.road.entity.marriage.MarriageInfo;
import com.yanqu.road.entity.marriage.MarriageRecord;
import com.yanqu.road.entity.marriage.MarriageResult;
import com.yanqu.road.logic.pb.MarriagePb;
import com.yanqu.road.pb.marriage.CrossMarriageProto;
import com.yanqu.road.pb.marriage.MarriageProto;
import com.yanqu.road.server.cmd.base.AbstractServerCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.marriage.MarriageMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;


@Cmd(code = Protocol.S_CROSS_UNION_MARRIAGE_AGREE_RESP, desc = "同意跨服商会联姻响应")
public class CrossUnionMarriageAgreeRespCmd extends AbstractServerCommand {
	private int result;
	private MarriageInfo marriageInfo;
	private MarriageInfo partnerMarriage = null;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CrossMarriageProto.CrossMarriageAgreeCrossDealRespMsg reqMsg = CrossMarriageProto.CrossMarriageAgreeCrossDealRespMsg.parseFrom(packet.getBytes());
		result = reqMsg.getRet();
		marriageInfo = MarriagePb.parseMarriageInfo(reqMsg.getApplyData());
		partnerMarriage = null;
		if(0 == result){
			partnerMarriage = MarriagePb.parseMarriageInfo(reqMsg.getPartnerData());
		}
		return true;
	}

	@Override
	public void execute(YanQuMessage packet) throws Exception {
		MarriageResult marriageResult = MarriageMgr.agreeCrossUnionMarriageResponse(result, marriageInfo, partnerMarriage);
		GamePlayer player = GamePlayerMgr.getOnlinePlayer(packet.getPlayerId());
		if(null != player){
			CrossMarriageProto.CrossMarriageApplyAgreeRespMsg.Builder respMsg = CrossMarriageProto.CrossMarriageApplyAgreeRespMsg.newBuilder();
			respMsg.setRet(marriageResult.getRet());
			if(0 == marriageResult.getRet()) {
				MarriageRecord marriageRecord = marriageResult.getMarriageRecord();
				if (null != marriageRecord) {
					String reward = PropertyHelper.parsePropertyToString(marriageRecord.getPartnerReward());
					respMsg.setReward(reward);
					MarriageProto.MarriageRecordTempMsg.Builder msg = MarriagePb.parseMarriageRecordTempMsg(marriageRecord);
					if (null != msg) {
						respMsg.setMarriageData(msg);
					}
				}
			}
			player.sendPacket(Protocol.U_CROSS_UNION_MARRIAGE_APPLY_AGREE, respMsg);
		}
	}

	@Override
	public void sendData(YanQuMessage packet) throws Exception {

	}
}
